/*
 * Copyright 2008-2012 Centro Andaluz de Innovación y Tecnologías de la
 * Información y las Comunicaciones (CITIC). All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Please contact CITIC - Edificio CITIC, C/ Marie Curie, 6 Parque
 * Tecnológico de Andalucía 29590 - Campanillas, Málaga, Spain
 * or visit www.citic.es, if you need additional information or
 * have any questions.
 */

package es.citic.puedes.youtube.impl;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * DataConfigManager
 * 
 * Data Configuration Manager
 *   
 */
public class DataConfigManager {

	/**
	 * Log instance
	 */
	private final Logger logger = LoggerFactory.getLogger(DataConfigManager.class);
	/**
	 * Data configuration xml file
	 */
	private Document configXmlFile = null;

	
	public DataConfigManager(String configFile){
		try {
			// Gets the xml
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(false);
			DocumentBuilder domBuilder = factory.newDocumentBuilder();
			configXmlFile = domBuilder.parse(new File(configFile));
		} catch (ParserConfigurationException e) {
			logger.warn("Can't load config xml file.");
			throw new RuntimeException("Can't load config xml file.");
		} catch (SAXException e) {
			logger.warn("Can't load config xml file.");
			throw new RuntimeException("Can't load config xml file.");
		} catch (IOException e) {
			logger.warn("Can't load config xml file.");
			throw new RuntimeException("Can't load config xml file.");
		}
	}

	/**
	 * Gets a value from the configuration xml file.
	 * 
	 * @param serviceName
	 *            The OSGi service name
	 * @param tagName
	 *            The tag from where to get the property
	 * @param propertyName
	 *            The property from where to get the value
	 * @return
	 * @throws CalendarException
	 */
	public String getValueFromConfigXml(String serviceName,String tagName, String propertyName) {

		String value = null;

		if (configXmlFile != null) {
			// Gets the root
			Element root = configXmlFile.getDocumentElement();
			NodeList serviceTagList = root.getElementsByTagName(serviceName);
			// Checks if the service exists
			if (serviceTagList != null && serviceTagList.getLength() != 0) {
				// Gets the service
				Element serviceTag = (Element) serviceTagList.item(0);
				// Searches the tag
				NodeList tagList = serviceTag.getElementsByTagName(tagName);
				// Checks if the tag exists
				if (tagList != null && tagList.getLength() != 0) {
					// Gets the tag
					Element tag = (Element) tagList.item(0);
					NodeList propertyList = tag.getElementsByTagName("property");

					int i = 0;
					boolean found = false;
					Element property_value = null;
					while (i < propertyList.getLength() && !found) {
						property_value = (Element) propertyList.item(i);
						found = property_value.getAttribute("id").equals(
								propertyName);
						i++;
					}

					if (found) {
						value = property_value.getFirstChild().getNodeValue();
					}
				}
			}
		} else {
			logger.warn("Data config xml file wasn't found.");
			throw new RuntimeException("Data config xml file wasn't found.");
		}

		return value;
	}
}